package online.sh.cn.fy.users.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import online.sh.cn.fy.common.utils.CookieUtil;
import online.sh.cn.fy.common.utils.PackageUtil;
import online.sh.cn.fy.common.utils.TokenUtils;
import online.sh.cn.fy.common.utils.WxUtils;

/**
 * 套餐手机，电渠接口查询
 */
@WebServlet("/packagecheck")
public class PackageCheckServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String code=request.getParameter("code");
		if (null!=code && code!="") {
			WxUtils wx=new WxUtils();
			JSONObject json=JSONObject.fromObject(wx.getOpenidAndToken(code));
			String openid=json.getString("openid");
			//数据库获取基础accesstoken
			TokenUtils to=new TokenUtils();
			String check=to.getUserMobile(openid);
			if(null!=check && ""!=check){
				String operator=wx.getMoblieinfo(check).getString("catName");
				check=CookieUtil.encodeCookie(check);
				CookieUtil.saveCookie(response, "shmobile", check);
				if (operator.equals("中国电信")) {
					String token=to.getshAccesstoken();
					String url=PackageUtil.getshUrl(token, "http://a6.189young.com/personalcenter/sendmobile");
					response.sendRedirect(url);
				}else{
					System.out.println("非电信用户手动绑定");
					response.sendRedirect("http://app.sh.189.cn/mwechat/query/package/use?channel=10033");
				}
			}else{
				System.out.println("未认证用户");
				response.sendRedirect("http://app.sh.189.cn/mwechat/query/package/use?channel=10033");
			}
		}else{
			response.sendRedirect("error.html");
		}
	}

}
